/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.model.ChannelAppPointofbilling;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class ChannelAppPointofbillingServiceImpl extends AbstractService implements ChannelAppPointofbillingService
{
	private Logger logger;
	
	public ChannelAppPointofbillingServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
		
	@Override
	public ChannelAppPointofbilling getChannelAppPointofbilling(int id) 
	{
		String sql = "select * from channelapppointofbilling where id=" + String.valueOf(id) + "";
		ModelGenerator<ChannelAppPointofbilling> dbcpmg = new ModelGenerator<ChannelAppPointofbilling>(ChannelAppPointofbilling.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<ChannelAppPointofbilling> getChannelAppPointofbillings()
	{
		String sql = "select * from channelapppointofbilling order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<ChannelAppPointofbilling> dbcpmg = new ModelGenerator<ChannelAppPointofbilling>(ChannelAppPointofbilling.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveChannelAppPointofbilling(Account operator, String type, List<ChannelAppPointofbilling> list,String channelappnumber)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql[] = new String[list.size()+1];
		sql[0]="delete from channelapppointofbilling where channelappnumber='"+channelappnumber+"'";
		ChannelAppPointofbilling cap = null;
		for(int i=0;i<list.size();i++)
		{
			cap = list.get(i);
			cap.setId(getTableId("channelapppointofbilling"));
			
			sql[i+1]="insert into channelapppointofbilling(id,apppointofbillingid,apppointofbillingcode,appnumber," +
					"channelappnumber,confirm,confirmtype,confirmstr) values " +
					"('"+cap.getId()+"','"+cap.getApppointofbillingid()+"','"+
					cap.getApppointofbillingcode()+"','"+cap.getAppnumber()+"','"
					+cap.getChannelappnumber()+"','"+cap.getConfirm()+"','"+cap.getConfirmtype()+"','"+cap.getConfirmstr()+"')";
		}
		return ConnectionManager.getInstance().executeBatch(dbcp, sql);
	}
}
